#include "dfmwidget.h"
#include "GPS_DFM_util.h"
#include <QtGui/QRegExpValidator>
#include <QtCore/QDebug>

DFMWidget::DFMWidget(QWidget *parent)
	: QWidget(parent)
	, currentTextType(GPS_LOC_TEXT_DFM)
	, current_lon(0)
	, current_lat(0)
{
	ui.setupUi(this);
	this->setAutoFillBackground(true);


	QRegExp lon_x("^-?(([1-9]\\d?)|(1[0-7]\\d))(\\.\\d{1,6})|180|0(\\.\\d{1,6})?$");
	QValidator *lon_validator = new QRegExpValidator(lon_x, this);
	ui.lineEdit_Lon_D->setValidator(lon_validator);

	QRegExp lat_x("^-?(([1-8]\\d?)|([1-8]\\d))(\\.\\d{1,6})|90|0(\\.\\d{1,6})?$");
	QValidator *lat_validator = new QRegExpValidator(lat_x, this);
	ui.lineEdit_Lat_D->setValidator(lat_validator);


	setGPSTextType(currentTextType);
	setLon(current_lon);
	setLat(current_lat);


	QList<QLineEdit *> lineEdits = findChildren<QLineEdit *>();
	for (int i = 0; i < lineEdits.size(); ++i) {
		QLineEdit* linedit = lineEdits.at(i);
		connect(linedit, SIGNAL(editingFinished()), this, SLOT(onLineEditingFinished()));
	}
}

DFMWidget::~DFMWidget()
{

}


void DFMWidget::setGPSTextType(GPSLocationTextType type) {
	ui.stackedWidget_Lon->setCurrentIndex(type);
	ui.stackedWidget_Lat->setCurrentIndex(type);
	currentTextType = type;
	setLon(current_lon);
	setLat(current_lat);

}
GPSLocationTextType DFMWidget::getGPSTextType() const
{
	return currentTextType;
}

void DFMWidget::setLon(double value)
{
	GPS_DFM_t dfm = D2DFM(value);
	ui.lineEdit_Lon_DFM_D->setText(QString::number(dfm.degree));
	ui.lineEdit_Lon_DFM_M->setText(QString::number(dfm.minute));
	ui.lineEdit_Lon_DFM_S->setText(QString::number(dfm.second, 'f', 2));

	GPS_DF_t df = D2DF(value);
	ui.lineEdit_Lon_DF_D->setText(QString::number(df.degree));
	ui.lineEdit_Lon_DF_F->setText(QString::number(df.minute, 'f', 3));

	ui.lineEdit_Lon_D->setText(QString::number(value, 'f', 6));

	current_lon = value;


}
double DFMWidget::getLon()
{
	return current_lon;
}

void DFMWidget::setLat(double value)
{
	GPS_DFM_t dfm = D2DFM(value);
	ui.lineEdit_Lat_DFM_D->setText(QString::number(dfm.degree));
	ui.lineEdit_Lat_DFM_M->setText(QString::number(dfm.minute));
	ui.lineEdit_Lat_DFM_S->setText(QString::number(dfm.second, 'f', 2));

	GPS_DF_t df = D2DF(value);
	ui.lineEdit_Lat_DF_D->setText(QString::number(df.degree));
	ui.lineEdit_Lat_DF_F->setText(QString::number(df.minute, 'f', 3));

	ui.lineEdit_Lat_D->setText(QString::number(value, 'f', 6));
	current_lat = value;

}
double DFMWidget::getLat()
{
	return current_lat;

}

void DFMWidget::onLineEditingFinished() {

	if (currentTextType == GPS_LOC_TEXT_DFM) {
		double d = ui.lineEdit_Lon_DFM_D->text().toDouble();
		double m = ui.lineEdit_Lon_DFM_M->text().toDouble();
		double s = ui.lineEdit_Lon_DFM_S->text().toDouble();
		GPS_DFM_t dfm;
		dfm.degree = d;
		dfm.minute = m;
		dfm.second = s;
		current_lon = DFM2D(dfm);


		d = ui.lineEdit_Lat_DFM_D->text().toDouble();
		m = ui.lineEdit_Lat_DFM_M->text().toDouble();
		s = ui.lineEdit_Lat_DFM_S->text().toDouble();
		dfm.degree = d;
		dfm.minute = m;
		dfm.second = s;
		current_lat = DFM2D(dfm);


	}
	else if (currentTextType == GPS_LOC_TEXT_D) {
		current_lon = ui.lineEdit_Lon_D->text().toDouble();
		current_lat = ui.lineEdit_Lat_D->text().toDouble();


	}
	else if (currentTextType == GPS_LOC_TEXT_DF) {
		double d = ui.lineEdit_Lon_DF_D->text().toDouble();
		double f = ui.lineEdit_Lon_DF_F->text().toDouble();
		GPS_DF_t df;
		df.degree = d;
		df.minute = f;

		current_lon = DF2D(df);

		d = ui.lineEdit_Lat_DF_D->text().toDouble();
		f = ui.lineEdit_Lat_DF_F->text().toDouble();

		df.degree = d;
		df.minute = f;

		current_lat = DF2D(df);

	}
	else {

	}

	emit LocationDataChanged(getLngLat());
	qDebug() << "LOC:" << getLngLat();
}

QPointF DFMWidget::getLngLat() const {
	return QPointF(current_lon, current_lat);
}